<?php

namespace App;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    //
    protected $guarded=[];
    protected $appends=[
        'status_text',
        'delivery_status_text',
        'receive_status_text',
        'address'
    ];
    protected $casts=[
        'coupon_id'=>'json'
    ];

    public function detail()
    {
        return $this->hasMany(OrderDetail::class,'order_id','order_number');
    }
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }
    public function getStatusTextAttribute()
    {
        switch ($this->status){
            case 2:
                return "已完成";
                break;
            case 1:
                switch ($this->deliveryStatus){
                    case 0:
                        return "已支付,未发货";
                        break;
                    case 1:
                        switch ($this->receivingStatus){
                            case 0:
                                return "已发货,未收货";
                                break;
                            case 1:
                                return "已收货";
                                break;
                        }
                        break;
                }
                break;
            case 0:
                return "待支付";
                break;
            case -1:
                return "已作废";
                break;
            case -2:
                return "已取消";
                break;
            case -3:
                return "已退款";
                break;
        }
    }
    public function getDeliveryStatusTextAttribute()
    {
        switch ($this->deliveryStatus){
            case 0:
                return "未发货";
                break;
            case 1:
                return "已发货";
                break;
        }
    }
    public function getReceiveStatusTextAttribute()
    {
        switch ($this->receivingStatus){
            case 0:
                return "未收货";
                break;
            case 1:
                return "已收货";
                break;
        }
    }
    public function getAddressAttribute()
    {
        return "收货地址：".$this->address_province.$this->address_city.$this->address_area.$this->address_detail.";收货人姓名:".$this->address_consignee.";手机号:".$this->address_phone_number;
    }
    public function user()
    {
        return $this->belongsTo(User::class,'uid','id');
    }
    public function share()
    {
        return $this->belongsTo(User::class,'share_uid','id');
    }
}
